#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QIcon>
#include "tcpmgr.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    _reg_dlg = nullptr;
    _reset_dlg = nullptr;
    ui->setupUi(this);
    this->setWindowTitle("chat");
    this->setWindowIcon(QIcon(":/icon.ico"));
    _login_dlg = new LoginDialog(this);
    _login_dlg->setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
    //创建和注册消息连接
    connect(_login_dlg,&LoginDialog::switchRegister,this,&MainWindow::SlotSwitchReg);//从登录界面到注册界面
    this->setCentralWidget(_login_dlg);
    _login_dlg->show();
    connect(_login_dlg,&LoginDialog::switchReset,this,&MainWindow::SlotSwitchReset);//从登录界面到忘记密码界面
    //从登录界面到聊天界面
    connect(TcpMgr::GetInstance().get(),&TcpMgr::sig_swich_chatdlg,this,&MainWindow::SlotSwitchChat);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::SlotSwitchReg()
{
    qDebug()<<"切换到注册界面";
    _reg_dlg = new registerDialog(this);
    connect(_reg_dlg,&registerDialog::sinals_switch_to_login,this,&MainWindow::SlotSwitchLogin);
    _reg_dlg->setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
    this->setCentralWidget(_reg_dlg);
    _login_dlg->hide();
    _reg_dlg->show();
}

void MainWindow::SlotSwitchLogin()
{
    qDebug()<<"切换到登录界面";
    _login_dlg = new LoginDialog(this);
    _login_dlg->setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
    connect(_login_dlg,&LoginDialog::switchRegister,this,&MainWindow::SlotSwitchReg);
    connect(_login_dlg,&LoginDialog::switchReset,this,&MainWindow::SlotSwitchReset);
    this->setCentralWidget(_login_dlg);
    if(_reg_dlg){
        _reg_dlg->hide();
        _reg_dlg=nullptr;
    }
    if(_reset_dlg){
        _reset_dlg->hide();
        _reset_dlg=nullptr;
    }

    _login_dlg->show();
}

void MainWindow::SlotSwitchReset()
{
    qDebug()<<"切换到忘记密码界面";
    _reset_dlg = new ResetDialog(this);
    connect(_reset_dlg,&ResetDialog::signals_switch_to_login,this,&MainWindow::SlotSwitchLogin);
    _reset_dlg->setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
    this->setCentralWidget(_reset_dlg);
    _login_dlg->hide();
    _reset_dlg->show();
}

void MainWindow::SlotSwitchChat()
{
    qDebug()<<"切换到聊天界面";
    _chat_dlg = new ChatDialog(this);
    _chat_dlg->setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
    this->setCentralWidget(_chat_dlg);
    _login_dlg->hide();
    _chat_dlg->show();
    this->setMinimumSize(QSize(950,800));
    this->setMaximumSize(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX);
}



